/*
       First routine is a simple ASL filerequester and the second does
       show you how to use SelectionList() to get a destination.

*/

#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/asl.h>
#include <proto/utility.h>
#include <proto/intuition.h>

extern struct Library *DOpusBase;

#include <dopus/modules.h>

/********************************************************************/
// since I did saw very strange stuff done also by average coders,
// I show you also how to work with a simple ASL filerequester...

#define TAGIF(expression,tag) ((expression) ? tag : TAG_IGNORE)

// a drawers only requester
#define FLG_DRAWER_ONLY       1 << 0

// requester should have half width of the supplied window, same height
// and should be centered...
#define FLG_CENTER            1 << 1


STRPTR GetFile( struct Window *win, ULONG flags, STRPTR title,
                STRPTR initial_path, STRPTR initial_file,
                STRPTR pattern, STRPTR dest )
{
        struct FileRequester *request;
        
        if( request = AllocAslRequestTags( ASL_FileRequest,
                                           ASLFR_Window, win,
                                           ASLFR_SleepWindow, TRUE,
                                           ASLFR_TitleText, title,
                                           TAGIF( initial_path, ASLFR_InitialDrawer ), initial_path,
                                           TAGIF( initial_file, ASLFR_InitialFile ), initial_file,
                                           TAGIF( pattern, ASLFR_InitialPattern ), pattern,
                                           TAGIF( flags & FLG_DRAWER_ONLY, ASLFR_DrawersOnly ), TRUE,
                                           TAGIF( flags & FLG_CENTER, ASLFR_InitialLeftEdge ), win->LeftEdge+win->Width/4,
                                           TAGIF( flags & FLG_CENTER, ASLFR_InitialTopEdge ), win->TopEdge ,
                                           TAGIF( flags & FLG_CENTER, ASLFR_InitialWidth ), win->Width/2 ,
                                           TAGIF( flags & FLG_CENTER, ASLFR_InitialHeight ), win->Height ,
                                           TAG_DONE ) )
          {
             if( AslRequest(request, NULL) )
               {
                  // at this place there exist many strange constructions...
                  // here is the style it should be done... !

                  GetCurrentDirName( dest, 256 );

                  // AddPart() does overwrite an existing path, if the part
                  // to add is already a complete path...

                  AddPart( dest, request->fr_Drawer, 256 );

                  if( !(flags & FLG_DRAWER_ONLY) )
                       AddPart( dest, request->fr_File, 256 );
               }
             else
               {
                  if( !IoErr() )
                       dest[0] = 0;
               }
                  
             FreeAslRequest( request );
          }
        return dest;
}

/********************************************************************/
// Usage of SelectionList()...


typedef
{
     IPCData           *ipc;          // your IPC pointer
     struct Screen     *screen;       // DOpus screen pointer
     Att_List          *destlist;     // list to hold all sources (not freed here !)
     DOpusCallbackInfo *dc;           // pointer (!) to the initialized structure

     /* receiving datas ... */

     APTR               destpath;     // choosed destination listerhandle
     struct Window     *destwin;      // the window pointer of it

     char               outpath[256]; // does contain afterwards the full destination path

}    Data;

// if this function does return NULL, this indicate the user has
// not choosed a dir
// if cd->destpath is NULL, the user has choosed a path where no destination
// lister exists.

// the needed locale strings should be no problem for you...

BOOL AskDest( Data *cd )
{
     short seldir = 0;
     APTR path;

     cd->destpath = NULL;
        
     cd->destlist = Att_NewList( LISTF_POOL );
                  
     while( (path = cd->dc->dc_GetDest(IPCDATA(cd->ipc), cd->outpath)) )
       {
          Att_NewNode( cd->destlist, cd->outpath, (ULONG) path, ADDNODEF_SORT|ADDNODEF_EXCLUSIVE);
          cd->dc->dc_EndDest( IPCDATA(cd->ipc), TRUE );
       }
                                  
     if( Att_NodeCount(cd->destlist) > 1 )
       {
          seldir = SelectionList( cd->destlist, NULL, cd->screen,
                                  DOpusGetString(locale, MSG_CHOOSE_DIR),
                                  0, SLF_DIR_FIELD, cd->outpath,
                                  DOpusGetString(locale, MSG_OKAY),
                                  DOpusGetString(locale, MSG_CANCEL) );

          if( seldir < 0 )
            {
               if( strlen(cd->outpath) )
                    return TRUE;

               return FALSE;
            }
                                          
          strcpy( cd->outpath, Att_NodeName(cd->destlist, seldir) );
       }
     else
          strcpy( cd->outpath, Att_NodeName(cd->destlist, 0) );
                
     cd->destpath = (APTR) Att_FindNode(cd->destlist, seldir)->data;
     cd->destwin  = cd->dc->dc_GetWindow( cd->destpath );

     return TRUE;
}